package com.aote.entity.component;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Element;

import com.aote.util.ComponentHelper;
import com.aote.util.EntityHelper;
import com.browsesoft.baseadapter.AdvancedSimpleEntity;
import com.browsesoft.dbtools.DBTools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.ReplacableParameterComponent;
import com.browsesoft.oa.DisposeSelectResult;

/**
 * 选择实体的一对多关系属性
 */
public class EntityAttributeSelectButton extends ReplacableParameterComponent
		implements DisposeSelectResult {
	/**
	 * 要选择的实体属性名
	 */
	private String attrName;

	// 初始实体ID号
	private String initEntities = new String();

	// 父实体
	private AdvancedSimpleEntity parent;

	/**
	 * onclick替换#ids#前的值，每次service方法要替换#ids#的值，所以必须保存
	 */
	private String onclick;

	public EntityAttributeSelectButton(HTMLBasicComponent parent)
			throws Exception {
		super(parent);
	}

	public void init(HttpServletRequest request, Element e) throws Exception {
		super.init(request, e);
		// 得到要选择的实体属性名
		attrName = e.getAttribute("property");
		// 替换组件名参数
		String onclick = e.getAttribute("onclick");
		onclick = onclick.replaceAll("#compid#", this.getName());
		// 替换#ids#内容为实际值
		this.onclick = onclick;
	}

	/**
	 * 替换ids为实际属性值
	 */
	private String replaceIds() {
		String onclick = new String(this.onclick);
		AdvancedSimpleEntity entity = (AdvancedSimpleEntity) this.getParent()
				.getModel();
		Iterator partEntities = entity.getPartEntities(attrName).iterator();
		// 使用不同版本，相同对象的标识号，以便不同版本的对象处理
		String ids = EntityHelper.entitiesToIDString(partEntities, "selfid");
		if (ids != null) {
			return onclick.replaceFirst("#ids#", ids);
		}
		return onclick;
	}

	public void dispose(String selectId) throws Exception {
		String entityType = this.getElement().getAttribute("entitytype");
		// 把选择结果给实体的属性
		AdvancedSimpleEntity entity = (AdvancedSimpleEntity) this.getParent()
				.getModel();
		List entities = EntityHelper.getEntities(entityType, selectId);
		entity.setPartEntities(attrName, entities);
		// 设置新的实体选择内容
		String onclick = this.replaceIds();
		this.getElement().setAttribute("onclick", onclick);
	}

	public void dispose(String name, String selectId) {
	}

	public void dispose(Hashtable ht) {
	}

	/**
	 * 
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.service(request, response);
		if (this.initEntities == null || this.initEntities.equals("")) {
			this.setInitEntities();
		}
		Element e = this.getElement();
		String onclick = this.replaceIds();
		e.setAttribute("onclick", onclick);
		String action = request.getParameter("action");
		if (action != null && action.equals("confirm")) {
			this.updateEntities();
		}
	}

	/**
	 * 设置
	 */
	private void setInitEntities() {
		this.parent = (AdvancedSimpleEntity) ComponentHelper
				.getFormEntity(this);
		Iterator iter = this.parent.getPartEntities(this.attrName).iterator();
		this.initEntities = EntityHelper.entitiesToIDString(iter, "selfid");
	}

	/**
	 * 更新选择实体
	 */
	private void updateEntities() {
		try {
			// 没有开始值，不替换
			if (this.initEntities != null && !this.initEntities.equals("")) {
				String unselect = this.getElement().getAttribute("unselect");
				String sqlo = unselect.replaceAll("#attrname#",
						this.initEntities);
				DBTools.excuteUpdate(sqlo);
			}
			Iterator iter = this.parent.getPartEntities(this.attrName)
					.iterator();
			String ids = EntityHelper.entitiesToIDString(iter, "selfid");
			// 如果没有值，不替换
			if (ids != null && !ids.equals("")) {
				String selected = this.getElement().getAttribute("selected");
				String sql = selected.replaceAll("#attrname#", ids);
				DBTools.excuteUpdate(sql);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}